package com.czl.module_service.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.bean.tengyun.AssetHandleSortBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemAssetHandleSortStoreBinding

class AssetHandleSortStoreAdapter :
    BaseQuickAdapter<AssetHandleSortBean, BaseDataBindingHolder<ItemAssetHandleSortStoreBinding>>(
        R.layout.item_asset_handle_sort_store
    ) {

    var productIdList: ArrayList<Int> = arrayListOf()
    var sortStoreIddList: ArrayList<String> = arrayListOf()
    var type: String = "store"

    override fun convert(
        holder: BaseDataBindingHolder<ItemAssetHandleSortStoreBinding>,
        item: AssetHandleSortBean,
    ) {
        holder.dataBinding?.run {
            if (item.manageMode == 1) {
                item.sortName = item.productName
                item.sortCode = item.productCode
                item.sortModel = item.productModel
                item.selectedInList = productIdList.contains(item.productId)
                item.sortNameTemp = item.sortName
                if (item.selected == null) {
                    item.selected = false
                }
                item.assetCount = 1
            } else {
                item.selectedInList = sortStoreIddList.contains(""+item.sortId + "_" + item.storehouseId)
                var sortNameTemp = item.sortName
                if (type == "store") {
                    sortNameTemp += "（"+ item.stockNum +"）"
                }
                item.sortNameTemp = sortNameTemp

            }
            item.inventoryBoolean = type == "inven" && item.manageMode == 2
            item.damageBoolean = type == "damage" && item.manageMode == 2
            item.storeBoolean = type == "store" && item.manageMode == 2
            item.selectChange = item.manageMode == 1 || type != "store"
            item.sortNo = item.sortCode
            item.model = item.sortModel
            item.houseStoreName = item.storehouseName
            item.locationDesc = item.storehouseLocatName
            item.position = holder.layoutPosition
            if (item.assetCount == null) {
                item.assetCount = 0
            }
            if (item.assetCount!! > 0) {
                item.assetCountStr = ""+item.assetCount
            }
            data = item
            adapter = this@AssetHandleSortStoreAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is AssetHandleSortBean) {
            if (!it.selectedInList!! && (it.manageMode == 1 || type != "store")) {
                var item: AssetHandleSortBean = it
                item.selected = !item.selected!!
                setData(item.position!!, item)
            }
        }
    })
}
